﻿/*** 1. 功能：验证码管理器抽象类
 *   2. 作者：Mix 
 *   3. 创建日期：2012-9-3
 *   4. 最后修改日期：2012-9-3
 *   5. 当前版本: 2012.9.3.1
***/
#region 引用命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
#endregion

namespace LX.Web.Security
{
    /// <summary>
    /// 验证码管理器抽象类
    /// </summary>
    public abstract class AbstractSafeCodeProvider : ISafeCodeProvider
    {
        /// <summary>
        /// 根据SESSIONID获取SafeCode
        /// </summary>
        /// <param name="sessionid">SESSIONID</param>
        /// <returns></returns>
        public abstract string GetSafeCode( string sessionid );

        /// <summary>
        /// 获取新验证码
        /// </summary>
        /// <param name="sessionid">SESSIONID</param>
        /// <returns></returns>
        public abstract string NewSafeCode( string sessionid );

        /// <summary>
        /// 清除验证码
        /// </summary>
        /// <param name="sessionid">SESSIONID</param>
        public abstract void ClearSafeCode( string sessionid );

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="sessionid">SESSIONID</param>
        /// <param name="safecode">验证码</param>
        /// <returns>是否验证成功</returns>
        public bool ValidSafeCode( string sessionid, string safecode )
        {
            string code = GetSafeCode( sessionid );
            if ( string.IsNullOrWhiteSpace( code ) || string.IsNullOrWhiteSpace( safecode ) )
            {
                return false;
            }
            else
            {
                ClearSafeCode( sessionid );
                return code.ToLower() == safecode.ToLower();
            }
        }
    }
}